Web calculator+Rapid Typing

calculator再回顾

学习

今天看了有解出这个题的博客,发现他写的脚本简单,且易理解,向他学习!还有意识到自己Python知识功底太差,以后要多加学习。

脚本代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import requests
from bs4 import BeautifulSoup
import re
s = requests.Session()
t = s.get('http://123.207.149.64:23331/calculator/')
#print(t.text)

#爬取网页里的<span>标签里的内容
soup = BeautifulSoup(t.text,'lxml')
a = soup.find_all(id="exp")
#打印出来,是一个只有一个元素的列表
print(a)
#把列表里的元素转为字符串
equ = str(a)


#对字符串里的纯数字进行提取
pattern = re.compile(r'\d+')
num = re.findall(pattern,equ)
#打印一下,看到数字列表
print(num)
j = 0
dic=[0,0,0,0]
for i in rangenum:
dic[j] = int(i)
j=j+1
result = dic[0]+dic[1]*dic[2]-dic[3]
canshu = {'answer':str(result)}
r =s.get('http://123.207.149.64:23331/calculator/', params=canshu)
print(r.text)

运行结果

下面是运行脚本的结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[<span id="exp">530076 + 102875 * 60812 - 45970 = </span>]
['530076', '102875', '60812', '45970']
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<style type="text/css">
.line_input{
border-width: 1px;
border-bottom: solid;
border-top: none;
border-left: none;
border-right: none;
border-width: 1px;
text-align: center;
outline: none;
margin: 0 1em;
}
</style>
</head>
<body>
<center>
<h1>Yet Another Calculator</h1>
flag{yes_you_are_calculat0r}<br />Time elapsed: 0.017600059509277 s<br />
</center>
</body>
</html>

Rapid Typing

题目描述

题目分析

  1. 在页面上随便输入几个数字,得到提交答案的URL以及参数名称
  2. 审查代码,发现要计算的数在img标签中,而且发现里边有Base64
  3. 提交结果用GET方法,参数code在URL中,最后以text形式查看。
  4. 第一次直接把提取出的img标签中的列表转换为字符串,然后解码,错误,发现要把base64后面的一长串字符串(这才是真正的base64编码后的内容)提取然后解码。
    错误代码:
    1
    2
    3
    4
    5
    6
    soup = BeautifulSoup(htm.text,'lxml')
    a=soup.find_all('img')
    print(a)
    equ = str(a)
    decod = base64.b64decode(equ)
    print(decod)

修改后代码

1
2
3
4
5
6
7
soup = BeautifulSoup(htm.text,'lxml')
#用逗号分隔字符串,取取第二块字符串(下标从0开始)
a = soup.find('img').get('src').split(',')[1]
print(a)
equ = str(a)
decod = base64.b64decode(equ)
print(decod)

输出结果如下

  1. 解码后发现是一个svg标签,复制整个标签代码到在线编辑器中,发现就是题目中的图片!百度后知道SVG是可缩放矢量图形(Scalable Vector Graphics)是基于可扩展标记语言(XML),用于描述二维矢量图形的一种图形格式。与传统的图像格式不同的是,SVG采用文本来描述矢量化的图形,这使得SVG图像文件可以像HTML网页一样有着很好的可读性。
    再看得到的svg标签,发现了图片中的字母和数字在text标签中,是二维的。
  2. 把text标签中的内容提取出来,并按x的大小排序后,取出text标签中的值,用一个字符串连接,打印一下,可以看到需要提交的字符串。
    排序参考:https://www.cnblogs.com/whaben/p/6495702.html
    取text标签中的内容,即需要输出的字符,可用bs4的string属性,具体可见:https://www.cnblogs.com/gl1573/p/9958716.html

脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import requests
from bs4 import BeautifulSoup
import re
import base64
session = requests.Session()
htm = session.get('http://123.207.149.64:23331/captcha/')
#print(htm.text)

#提取img标签内容(仅提取base64后面的内容,即逗号后边第一个)
soup = BeautifulSoup(htm.text,'lxml')
a = soup.find('img').get('src').split(',')[1]
#print(a)
stra = str(a)

#base64解码
decod = base64.b64decode(stra)
#print(decod)
#提取解码后svg中的所有text标签
soup1 = BeautifulSoup(decod,'lxml')
tex = soup1.find_all("text")
#print(tex)

#对x进行排序(text标签中)
sortx = sorted(tex,key=lambda x:int(x.get('x')))
#print (sortx)

#用string属性提取text标签中的字符,并用string1进行连接
string1=''
for zimu in sortx:
string1 = string1+zimu.string
print(string1)

#提交结果
result1 = {'code': string1}
r =session.get("http://123.207.149.64:23331/captcha/", params=result1)
print(r.text)

提交结果

1
2
3
4
5
6
7
8
9
10
qeJYoNRiXYUpXKawgjysh5NHY3BxaMZeeMPZa6Lr
<!DOCTYPE html>
<html>
<head>
<title>Rapid Typing</title>
</head>
<body>
<h1>Rapid Typing</h1>
flag{svg_C4P7cHa_n0t_$ecUr3}<br />Time elapsed: 0.1487181186676 s<br /></body>
</html>
文章目录
  1. 1. calculator再回顾
    1. 1.1. 学习
    2. 1.2. 脚本代码
    3. 1.3. 运行结果
  2. 2. Rapid Typing
    1. 2.1. 题目描述
    2. 2.2. 题目分析
    3. 2.3. 脚本
    4. 2.4. 提交结果